package com.xutong.myfunction.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.xutong.myfunction.R;
import com.xutong.myfunction.fragment.IndicatorFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tongtian on 2017/9/21.
 */
public class PageIndicatorActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TabLayout tabLayout;

    private List<String> list;
    private List<String> listTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pageindicator);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        list = new ArrayList<>();
        for (int i=0;i<8;i++){
            list.add("内容"+i);
        }
        listTitle = new ArrayList<>();
        for (int i=0;i<8;i++){
            listTitle.add("标题"+i);
        }
        MyPageAdapter myPageAdapter = new MyPageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(myPageAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }


    class MyPageAdapter extends FragmentPagerAdapter{

        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            IndicatorFragment indicatorFragment = new IndicatorFragment();
            Bundle bundle = new Bundle();
            bundle.putString("text",list.get(position));
            indicatorFragment.setArguments(bundle);
            return indicatorFragment;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return listTitle.get(position);
        }
    }

}
